﻿using AutoMapper;
using Compass.Wasm.Server.Services.Identities;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Wms;
using Compass.WmsService.Domain;
using Compass.WmsService.Domain.Entities;
using Compass.WmsService.Infrastructure;

namespace Compass.Wasm.Server.Services.Wms;
public interface IStockOutService : IBaseService<StockOutDto>
{
    Task<ApiResponse<IEnumerable<StockOutDto>>> GetFromLastByCargoAsync(Guid cargoId, DateTime last);
    Task<ApiResponse<IEnumerable<StockOutDto>>> GetAllByCargoAsync(Guid cargoId);
    Task<ApiResponse<IEnumerable<StockOutDto>>> GetAllByPeriodAsync(DateTime start, DateTime end);
}
public class StockOutService : IStockOutService
{
    #region ctor
    private readonly WmsDbContext _dbContext;
    private readonly IWmsRepository _repository;
    private readonly IMapper _mapper;
    private readonly IUserService _userService;
    public StockOutService(WmsDbContext dbContext, IWmsRepository repository, IMapper mapper, IUserService userService)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
        _userService = userService;
    }
    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<StockOutDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetStockOutsAsync();
            //按照入库时间
            var dtos = await _mapper.ProjectTo<StockOutDto>(models).OrderBy(x => x.OutTime).ToListAsync();
            //查询物料信息
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByIdAsync(dto.CargoId);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
                dto.Unit=cargo.Unit;
                dto.Location = cargo.Location;
                dto.Operator= (await _userService.GetSingleAsync(dto.OperatorId)).Result.UserName;
            }
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<StockOutDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetStockOutByIdAsync(id);
            if (model == null) return new ApiResponse<StockOutDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<StockOutDto>(model);

            var cargo = await _repository.GetCargoByIdAsync(dto.CargoId);
            if (cargo!=null)
            {
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
                dto.Unit = cargo.Unit;
                dto.Location = cargo.Location;
                dto.Operator = (await _userService.GetSingleAsync(dto.OperatorId)).Result.UserName;
            }

            return new ApiResponse<StockOutDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<StockOutDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<StockOutDto>> AddAsync(StockOutDto dto)
    {
        try
        {
            var model = new StockOut(Guid.NewGuid(), dto.CargoId, dto.Station, dto.OdpNumber, dto.TransitQuantity, dto.Quantity, dto.ExpectedTime, dto.OutTime, dto.OperatorId, dto.Remarks);
            await _dbContext.StockOuts.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<StockOutDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<StockOutDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<StockOutDto>> UpdateAsync(Guid id, StockOutDto dto)
    {
        try
        {
            var model = await _repository.GetStockOutByIdAsync(id);
            if (model == null) return new ApiResponse<StockOutDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<StockOutDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<StockOutDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<StockOutDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetStockOutByIdAsync(id);
            if (model == null) return new ApiResponse<StockOutDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<StockOutDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<StockOutDto> { Status = false, Message = e.Message };
        }
    }
    #endregion

    #region 扩展查询方法
    public async Task<ApiResponse<IEnumerable<StockOutDto>>> GetFromLastByCargoAsync(Guid cargoId, DateTime last)
    {
        try
        {
            var models = await _repository.GetStockOutsFromLastByCargoAsync(cargoId, last);
            //按照库位排序
            var dtos = await _mapper.ProjectTo<StockOutDto>(models).OrderBy(x => x.OutTime).ToListAsync();
            //查询物料信息
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByIdAsync(dto.CargoId);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
                dto.Unit=cargo.Unit;
                dto.Location = cargo.Location;
                dto.Operator= (await _userService.GetSingleAsync(dto.OperatorId)).Result.UserName;
            }
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<StockOutDto>>> GetAllByCargoAsync(Guid cargoId)
    {
        try
        {
            var models = await _repository.GetStockOutsByCargoAsync(cargoId);
            //按照库位排序
            var dtos = await _mapper.ProjectTo<StockOutDto>(models).OrderBy(x => x.OutTime).ToListAsync();
            //查询物料信息
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByIdAsync(dto.CargoId);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
                dto.Unit=cargo.Unit;
                dto.Location = cargo.Location;
                dto.Operator= (await _userService.GetSingleAsync(dto.OperatorId)).Result.UserName;
            }
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<StockOutDto>>> GetAllByPeriodAsync(DateTime start, DateTime end)
    {
        try
        {
            var models = await _repository.GetStockOutsByPeriodAsync(start, end);
            //按照库位排序
            var dtos = await _mapper.ProjectTo<StockOutDto>(models).OrderBy(x => x.OutTime).ToListAsync();
            //查询物料信息
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByIdAsync(dto.CargoId);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
                dto.Unit=cargo.Unit;
                dto.Location = cargo.Location;
                dto.Operator= (await _userService.GetSingleAsync(dto.OperatorId)).Result.UserName;
            }
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<StockOutDto>> { Status = false, Message = e.Message };
        }
    }
    #endregion
}